from appium import webdriver
from appium.options.android import UiAutomator2Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

desired_caps = {
    "platformName": "Android",
    "platformVersion": "13",
    "deviceName": "Genymotion Virtual Device",
    "appPackage": "com.android.settings",
    "appActivity": ".Settings",
    "automationName": "UiAutomator2",
    "noReset": True,
    "newCommandTimeout": 60
}
options=UiAutomator2Options().load_capabilities(desired_caps)
driver=webdriver.Remote(
    command_executor='http://127.0.0.1:4723/wd/hub',
    options=options
)
try:
    # 等待设置页面加载完成
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, 'android.widget.TextView'))
    )

    # 保存截图用于调试
    driver.save_screenshot('settings_page.png')

    # 打印页面源
    print("页面源:", driver.page_source)

    # 使用显式等待和更灵活的 XPath
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[contains(@text, "About")]'))
    )

    # 执行操作
    element.click()

except Exception as e:
    print(f"错误: {e}")
    driver.save_screenshot('error.png')  # 保存错误截图
finally:
    driver.quit()